package com.lei.sort;

import com.lei.linearsearch.ArrayGenerator;

/**
 * @Author LeiXiaoLei
 * @Date 2022/8/23 16:26
 * @Version 1.0
 */
public class ShellSort {
    private ShellSort() {
    }

    public static <E extends Comparable<E>> void sort(E[] data) {
        int h = 1;
        while (h < data.length) h = h * 3 + 1;
        while (h >= 1) {
            for (int i = h; i < data.length; i++) {
                E t = data[i];
                int j;
                for (j = i; j - h >= 0 && t.compareTo(data[j - h]) < 0; j -= h) {
                    data[j] = data[j - h];
                }
                data[j] = t;
            }
            h = (h - 1) / 3;
        }
    }

    public static <E extends Comparable<E>> void sort3(E[] data) {
        int h = data.length / 2;
        while (h >= 1) {
            for (int i = h; i < data.length; i++) {
                E t = data[i];
                int j;
                for (j = i; j - h >= 0 && t.compareTo(data[j - h]) < 0; j -= h) {
                    data[j] = data[j - h];
                }
                data[j] = t;
            }
            h /= 2;
        }
    }

    public static <E extends Comparable<E>> void sort2(E[] data) {
        int h = data.length / 2;
        while (h >= 1) {
            for (int start = 0; start < h; start++) {
                for (int i = start + h; i < data.length; i += h) {
                    E t = data[i];
                    int j;
                    for (j = i; j - h >= 0 && t.compareTo(data[j - h]) < 0; j -= h) {
                        data[j] = data[j - h];
                    }
                    data[j] = t;
                }
            }
            h /= 2;
        }
    }


    public static void main(String[] args) {
        int[] dataSize = {10, 100, 1000, 10000, 100000, 1000000, 10000000};
        for (int n : dataSize) {
            Integer[] arr = ArrayGenerator.generatorRandomArray(n, n);
            SortingHelper.sortTest(ShellSort.class, arr);
        }
    }
}
